/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Graphics;

import org.lwjgl.opengl.*;
import org.lwjgl.*;
import Settings.*;
import java.lang.Math.*;
import org.lwjgl.util.*;

/**
 *
 * @author Klaus
 */
public class Sprite
{
    private static Texture texture;
    private static TextureLoader tl;

    public Sprite()
    {
    }
    
    public static void init()
    {
        tl = new TextureLoader();
    }

    public static void setTexture( Texture newTexture )
    {
        texture = newTexture;
    }

    public static void preloadSprites()
    {
        //TODO preload all sprites
    }

    public static void setTexture( String newTexture )
    {
        try
        {
            setTexture( tl.getTexture( newTexture ) );
        }
        catch( Exception e )
        {
            Sys.alert( "Error" , "Not able to load texture!" );
            e.printStackTrace();
        }
    }

    public static void draw( int x , int y , int size )
    {
        GL11.glPushMatrix();

        if( texture == null )
            return;

        texture.bind();

        GL11.glTranslatef( x , y , 0 );
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glColor4f(1f, 1f, 1f, 1f);

        // draw a quad textured to match the sprite
        GL11.glBegin( GL11.GL_QUADS );
        {
            GL11.glTexCoord2f( 0 , 0);
            GL11.glVertex2f( 0 , 0 );
            GL11.glTexCoord2f( 0 , texture.getHeight() );
            GL11.glVertex2f( 0 , size );
            GL11.glTexCoord2f( texture.getWidth() , texture.getHeight() );
            GL11.glVertex2f( size , size );
            GL11.glTexCoord2f( texture.getWidth() , 0 );
            GL11.glVertex2f( size , 0 );
        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public static void draw( int x , int y , int sizeX , int sizeY )
    {
        GL11.glPushMatrix();

        if( texture == null )
            return;

        texture.bind();

        GL11.glTranslatef( x , y , 0 );
        GL11.glColor3f( 1 , 1 , 1 );

        // draw a quad textured to match the sprite
        GL11.glBegin( GL11.GL_QUADS );
        {
            GL11.glTexCoord2f( 0 , 0);
            GL11.glVertex2f( 0 , 0 );
            GL11.glTexCoord2f( 0 , texture.getHeight() );
            GL11.glVertex2f( 0 , sizeY );
            GL11.glTexCoord2f( texture.getWidth() , texture.getHeight() );
            GL11.glVertex2f( sizeX , sizeY );
            GL11.glTexCoord2f( texture.getWidth() , 0 );
            GL11.glVertex2f( sizeX , 0 );
        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public static void drawClean( int x , int y , int size )
    {
        GL11.glPushMatrix();

        GL11.glTranslatef( x , y , 0 );
        GL11.glColor3f( 1 , 1 , 1 );

        // draw a quad textured to match the sprite
        GL11.glBegin( GL11.GL_QUADS );
        {
            GL11.glVertex2f( 0 , 0 );
            GL11.glVertex2f( 0 , size );
            GL11.glVertex2f( size , size );
            GL11.glVertex2f( size , 0 );
        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public static void drawClean( int x , int y , int sizeX , int sizeY )
    {
        GL11.glPushMatrix();

        GL11.glTranslatef( x , y , 0 );
        GL11.glColor3f( 1 , 1 , 1 );

        // draw a quad textured to match the sprite
        GL11.glBegin( GL11.GL_QUADS );
        {
            GL11.glVertex2f( 0 , 0 );
            GL11.glVertex2f( 0 , sizeY );
            GL11.glVertex2f( sizeX , sizeY );
            GL11.glVertex2f( sizeX , 0 );
        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public static void drawTriangle( Dimension v1 , Dimension v2 , Dimension v3 )
    {
        GL11.glPushMatrix();

        GL11.glColor3f( 0f , 0f , 1f );

        // draw a triangle
        GL11.glBegin( GL11.GL_TRIANGLES );
        {
            GL11.glVertex2f( v1.getWidth() , v1.getHeight() ); // links oben
            GL11.glVertex2f( v2.getWidth() , v2.getHeight() ); // 0,5 höhe ,
            GL11.glVertex2f( v3.getWidth() , v3.getHeight() );
        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public static void drawNewTexture( String filename , int x , int y , int size )
    {
        if( filename != null )
        {
            setTexture( filename );
            draw( x , y , size );
        }
        else
            drawClean( x , y , size );
    }

    public static void drawNewTexture( String filename , int x , int y , int sizeX , int sizeY )
    {
        if( filename != null )
        {
            setTexture( filename );
            draw( x , y , sizeX, sizeY );
        }
        else
            drawClean( x , y , sizeX , sizeY );
    }

    public static void drawNewTexture( Texture tx , int x , int y , int size )
    {
        if( tx != null )
        {
            setTexture( tx );
            draw( x , y , size );
        }
        else
            drawClean( x , y , size );
    }

    public static TextureLoader returnTL()
    {
        return tl;
    }
    
}
